package cn.stylefeng.guns.modular.business.frequency.orderfinishimage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderfinishimage.entity.OrderFinishImage;
import cn.stylefeng.guns.modular.business.frequency.orderfinishimage.mapper.OrderFinishImageMapper;
import cn.stylefeng.guns.modular.business.frequency.orderfinishimage.model.param.OrderFinishImageParam;
import cn.stylefeng.guns.modular.business.frequency.orderfinishimage.service.OrderFinishImageService;
import cn.stylefeng.guns.modular.business.frequency.orderfinishimage.enums.OrderFinishImageExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单交付图片 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderFinishImageServiceImpl extends ServiceImpl<OrderFinishImageMapper, OrderFinishImage> implements OrderFinishImageService {

    @Override
    public PageResult<OrderFinishImage> page(OrderFinishImageParam orderFinishImageParam) {

        // 构造条件
        LambdaQueryWrapper<OrderFinishImage> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderFinishImage> list(OrderFinishImageParam orderFinishImageParam) {

        // 构造条件
        LambdaQueryWrapper<OrderFinishImage> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderFinishImageParam orderFinishImageParam) {

        // 将dto转为实体
        OrderFinishImage orderFinishImage = new OrderFinishImage();
        BeanUtil.copyProperties(orderFinishImageParam, orderFinishImage);

        this.save(orderFinishImage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderFinishImageParam orderFinishImageParam) {
        this.removeById(orderFinishImageParam.getId());
    }

    @Override
    public void edit(OrderFinishImageParam orderFinishImageParam) {

        // 根据id查询实体
        OrderFinishImage orderFinishImage = this.queryOrderFinishImage(orderFinishImageParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderFinishImageParam, orderFinishImage);

        this.updateById(orderFinishImage);
    }

    @Override
    public OrderFinishImage detail(OrderFinishImageParam orderFinishImageParam) {
        return this.queryOrderFinishImage(orderFinishImageParam);
    }



    /**
     * 获取订单交付图片
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderFinishImage queryOrderFinishImage(OrderFinishImageParam orderFinishImageParam) {
        OrderFinishImage orderFinishImage = this.getById(orderFinishImageParam.getId());
        if (ObjectUtil.isEmpty(orderFinishImage)) {
            throw new ServiceException(OrderFinishImageExceptionEnum.NOT_EXISTED);
        }
        return orderFinishImage;
    }
}